package pl.edu.agh.iosr.gamblingzone.mainpanel.action;

import static org.jboss.seam.ScopeType.SESSION;

import java.util.LinkedList;
import java.util.List;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.faces.component.UIData;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.security.Restrict;
import org.richfaces.event.NodeSelectedEvent;

import pl.edu.agh.iosr.gamblingzone.dao.AbstractEventDAO;
import pl.edu.agh.iosr.gamblingzone.dao.GroupDAO;
import pl.edu.agh.iosr.gamblingzone.model.AbstractEvent;
import pl.edu.agh.iosr.gamblingzone.model.Bet;
import pl.edu.agh.iosr.gamblingzone.model.Group;
import pl.edu.agh.iosr.gamblingzone.model.Participant;
import pl.edu.agh.iosr.gamblingzone.model.User;
import pl.edu.agh.iosr.gamblingzone.utils.DAOFactory;
import pl.edu.agh.iosr.gamblingzone.utils.EJBDAOFactoryBean;

@Stateful
@Scope(ScopeType.SESSION)
@Name("eventsView")
@Restrict("#{identity.loggedIn}")
public class EventsViewAction implements EventsView {
	
	@PersistenceContext
	private EntityManager em;
	
	@In(required=true)
	DAOFactory dao;
	
	@Out(required = false, scope = SESSION)
	private Bet mybet;
	
	private List<Bet> bets;
	
	private List<AbstractEvent> events = new LinkedList<AbstractEvent>();
	
	private Participant firstParticipant;
	private Participant secondParticipant;

	public List<AbstractEvent> getEvents() {
		return events;
	}

	public String printEvent(AbstractEvent ae) {

		String outParticipants = "";

		for (Participant p : ae.getRates().keySet()) {
			outParticipants += p.getName()+" - ";
		}
		outParticipants = outParticipants.substring(0, outParticipants.length()-3);
		
		refreshBet(ae);

		return outParticipants;
	}

	public void setEvents(List<AbstractEvent> events) {
		this.events = events;
	}

	@Create
	public void init() {
		// TODO Auto-generated method stub
		events = new LinkedList<AbstractEvent>();
	}
	
	@Remove
	public void remove() {
		// TODO Auto-generated method stub
		
	}

	public void setActiveGroup(Group g) {
		Group currGroup = g;
		System.out.println("===================== O.K	 ==========================");
		
		if (dao == null) {
			dao = new EJBDAOFactoryBean();
		}
		GroupDAO groupDAO = dao.getGroupDAO();
		currGroup = groupDAO.makePersistent(currGroup);
		events = currGroup.getEvents();
	}

	public String getFirstRate(AbstractEvent ae) {
        for (Participant p: ae.getRates().keySet()) {
            firstParticipant = p;
        }
        
        return String.format("%.2f", (1.0 / (0.1 + ae.getRates().get(firstParticipant))));
    }

	public String getSecondRate(AbstractEvent ae) {
        for (Participant p: ae.getRates().keySet()) {
            if (!p.equals(firstParticipant)) {
                secondParticipant = p;
            }
        }
        
        return String.format("%.2f", (1.0 / (0.1 + ae.getRates().get(secondParticipant))));
    }

	public Bet bet1(AbstractEvent ae) {
		System.out.println("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW");
		Bet tmpBet = new Bet();
		tmpBet.addEvent(ae);
		tmpBet.setWinner(firstParticipant);
		return tmpBet;
	}

	public Bet bet2(AbstractEvent ae) {
		System.out.println("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW");
		Bet tmpBet = new Bet();
		tmpBet.addEvent(ae);
		tmpBet.setWinner(secondParticipant);
		return tmpBet;
	}

	public List<Bet> getBets() {
		return bets;
	}

	public void setBets(List<Bet> bets) {
		this.bets = bets;
	}

	public Bet getMyBet() {
		return mybet;
	}

	public void setMyBet(Bet myBet) {
		this.mybet = myBet;
	}

	public void refreshBet(AbstractEvent ae) {
		System.out.println("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW");
		System.out.println("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW");
		System.out.println("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW");
		System.out.println("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW");
		System.out.println("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW");
		if (firstParticipant != null) {
			System.out.println("WWWWWWWWWWWWWWWWWW////////////////////////////WWWWWWWWWWWWWWWWWW");
//			mybet = new Bet();
			mybet.setWinner(firstParticipant);
			mybet.getEvents().clear();
			mybet.addEvent(ae);
		} else {
			mybet = new Bet();
		}
	}
}
